package com.xing.common.vail;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 邢福豪
 * @date 2022/9/30 13:35
 *
 * 自定义校验器
 *
 */


//泛型左边：自定义校验注解，泛型右边：校验的类型
public class ListValueConstraintValidator
        implements ConstraintValidator<ListValue,Integer> {


    private Set<Integer> set = new HashSet<Integer>();


    /**
     * 初始化方法
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {

        int[] value = constraintAnnotation.value();

        for (int val : value) {
            set.add(val);

        }

    }


    /**
     * 判断是否校验成功
     * @param value
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {

        //判断是否包含的值
        boolean contains = set.contains(value);
        return contains;
    }
}
